package com.me.study.jdk8.date;

import org.apache.commons.lang3.time.DurationFormatUtils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

/**
 * Java8：Duration 测试类 -- 表示两个时刻之间的时间间隔
 *
 * @author ME
 * @date 2021/2/19
 */
public class DurationTest {

    public static void main(String[] args) {
        // 构建 LocalDateTime对象
        LocalDateTime start = LocalDateTime.of(2019, 12, 31, 0, 0, 0);
        LocalDateTime end = LocalDateTime.of(2020, 1, 1, 1, 3, 29);

        // 求 LocalDateTime 之间的时间差（PT1235H10M30S），需转换为其他格式
        // Duration 和 Period 的表示方法也符合 ISO 8601的格式，
        // 它以 `P...T...` 的形式表示，`P...T` 之间表示日期间隔，`T` 后面表示时间间隔。如果是 `PT...` 格式表示仅有时间间隔
        Duration duration = Duration.between(start, end);
        // PT25H3M9S
        System.out.println("Duration.between('2019-12-31 0:0:00', '2020-1-1 1:1:9'): " + duration.toString());

        System.out.println("相差 1天1小时3分钟29秒");
        System.out.println("相差的天数: " + duration.toDays());// 1
        System.out.println("相差的小时数: " + duration.toHours());// 25
        System.out.println("相差的分钟数: " + duration.toMinutes());// 1503
        System.out.println("相差的秒数: " + duration.getSeconds());// 90209
        System.out.println("相差的毫秒数: " + duration.toMillis());// 90209000
        System.out.println("相差的纳秒数: " + duration.toNanos());// 90209000000000
        System.out.println("相差的时间 toString: " + duration.toString());// PT25H3M9.0004251S

        System.out.println("\n格式化相差的时间: " + DurationFormatUtils.formatDuration(duration.toMillis(),
                "d天H小时m分钟s秒",false));// 1天1小时3分钟29秒
        Duration duration1 = duration.plusMinutes(20);
        System.out.println("duration 增加20分钟，本身不变: " + DurationFormatUtils.formatDuration(duration.toMillis(),
                "d天H小时m分钟s秒",false));// 1天1小时3分钟29秒
        System.out.println("duration 增加20分钟，返回值才会变: " + DurationFormatUtils.formatDuration(duration1.toMillis(),
                "d天H小时m分钟s秒",false));// 1天1小时23分钟29秒
        System.out.println();

        System.out.println("相差的年数: " + ChronoUnit.YEARS.between(start, end));// 0
        System.out.println("相差的天数: " + ChronoUnit.DAYS.between(start, end));// 1
        System.out.println("相差的小时数: " + ChronoUnit.HOURS.between(start, end));// 25
        System.out.println("相差的分钟数: " + ChronoUnit.MINUTES.between(start, end));// 1503

        System.out.println("\nLocalTime Duration");
        LocalTime time1 = LocalTime.of(9, 0, 0);
        LocalTime time2 = LocalTime.of(10, 30, 0);
        Duration duration2 = Duration.between(time1, time2);
        // PT1H30M
        System.out.println(duration2);
        // PT22H30M
        System.out.println(Duration.ofDays(1).minus(duration2));
        // PT-1H-30M
        System.out.println(Duration.between(time2, time1));
        // PT22H30M
        System.out.println(Duration.ofDays(1).plus(Duration.between(time2, time1)));

        System.out.println("\n1天 == 24小时? true");
        System.out.println(Duration.ofDays(1).equals(Duration.ofHours(24)));

        duration = Duration.ofSeconds( 9, 425169);
        System.out.printf("\n相差 [%d]秒\n", duration.getSeconds());// 相差 [9]秒
        System.out.printf("相差 [%d]纳秒\n", duration.toNanos());// 相差 [9000425169]纳秒
        System.out.println("纳秒位的差值: " + duration.getNano());// 425169
        System.out.printf("相差 [%d]纳秒\n", (duration = duration.withSeconds(0)).toNanos());// 相差 [425169]纳秒
        System.out.printf("相差 [%d]纳秒\n", duration.withNanos(0).toNanos());// 相差 [0]纳秒

        System.out.println("\n差多少时间: " + (duration = Duration.ofMillis(9512)));
        // 非整秒数则 +1
        if (duration.toMillis() % 1000 > 0) {
            System.out.println("差多少时间（凑整秒）: " + Duration.ofSeconds(duration.getSeconds() + 1));
        }
    }

}
